import logging
import pathlib
import threading
from logging.handlers import RotatingFileHandler

from utils import read_yaml, produce, schedule

root_dir = pathlib.Path().absolute()
config_filename = root_dir.joinpath('config.yaml')
logs_dir = root_dir.joinpath("logs")
logs_dir.mkdir(exist_ok=True)

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s.%(msecs)d - %(process)d:%(thread)d:%(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    handlers=[
        RotatingFileHandler(
            filename='logs/main.txt', encoding='utf-8',
            maxBytes=1024 * 1024 * 10, backupCount=5,
        ),
        logging.StreamHandler(),
    ],
)


def main():
    config_file = read_yaml(config_filename)
    local_path = config_file.get('path')
    root_url = config_file.get('url')
    v_projects = config_file.get('vehicle_projects', [])
    t = threading.Thread(
        target=produce,
        args=(local_path, root_url, v_projects,)
    )
    t1 = threading.Thread(target=schedule)
    t.start()
    t1.start()
    t.join()
    t1.join()


if __name__ == '__main__':
    main()
